package com.data.monitoring.controller;

import com.data.monitoring.model.DTO.MonitoredFolderDTO;
import com.data.monitoring.model.entity.MonitoredFolders;
import com.data.monitoring.model.entity.Servers;
import com.data.monitoring.service.MonitoredFoldersService;
import com.data.monitoring.service.ServersService;
import com.data.monitoring.service.impl.FolderMonitorScheduler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 监控文件夹配置 前端控制器
 * </p>
 *
 * @author HanYiMing
 * @since 2025-05-28
 */
@RestController
@RequestMapping("/api/monitored-folders")
@Api(tags = "监听信息控制类")
public class MonitoredFoldersController {

    @Autowired
    private MonitoredFoldersService folderRepository;

    @Autowired
    private FolderMonitorScheduler monitorScheduler;


    @Autowired
    private ServersService serversService;

    /**
     * 添加单个监控任务
     */
    @PostMapping("addFolder")
    @ApiOperation(value = "添加监听文件夹信息")
    public ResponseEntity<MonitoredFolders> addFolder(@Valid @RequestBody MonitoredFolderDTO dto) {

        Servers server = serversService.getById(dto.getServerId());
        MonitoredFolders folder = new MonitoredFolders();
        folder.setServers(server);
        folder.setFolderPath(dto.getFolderPath());
        folder.setCheckInterval(dto.getCheckInterval());
        folder.setMaxDepth(dto.getMaxDepth());
        folder.setActive(true);
        folder.setServerId(server.getServerId());

        folderRepository.save(folder);
        monitorScheduler.scheduleMonitor(folder);

        return ResponseEntity.created(URI.create("/api/monitored-folders/" + folder.getFolderId())).body(folder);
    }

    /**
     * 批量初始化监控任务
     */
    @PostMapping("/batch-init")
    public ResponseEntity<List<MonitoredFolders>> batchAddFolders(@Valid @RequestBody List<MonitoredFolderDTO> dtos) {

        List<MonitoredFolders> result = new ArrayList<>();

        for (MonitoredFolderDTO dto : dtos) {
            Servers server = serversService.getById(dto.getServerId());
            MonitoredFolders folder = new MonitoredFolders();
            folder.setServers(server);
            folder.setServerId(server.getServerId());
            folder.setFolderPath(dto.getFolderPath());
            folder.setCheckInterval(dto.getCheckInterval());
            folder.setMaxDepth(dto.getMaxDepth());
            folder.setActive(true);
            folderRepository.save(folder);
            monitorScheduler.scheduleMonitor(folder);
            result.add(folder);
        }

        return ResponseEntity.ok(result);
    }
}
